package com.monitor.controller;

import javax.servlet.http.HttpSession;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.monitor.model.Admin;
import com.monitor.model.Log;
import com.monitor.service.Alarm;
import com.monitor.utils.PropertyUtil;
import com.monitor.utils.SHA256Util;


public class ManagerController extends Controller {
	private static final Logger log = Log4jLogger.getLogger(IndexController.class);
	
	@ActionKey("/manager")
	public void managerview(){
		log.debug("进入ActionKey manager ");
		
		if(getSessionAttr("login") == null?true:false){
			redirect("/lock");
			return;
		}
		PropertyUtil property = new PropertyUtil("config.properties", "UTF-8");
		setAttr("normaltem", property.get("normalTemperature"));
		setAttr("normalhum", property.get("normalHumidity"));
		setAttr("interval", property.get("interval"));
		setAttr("phone", property.get("phone"));
		setAttr("appkey", property.get("appKey"));
		setAttr("secondSpace", property.get("secondSpace"));
		render("/WEB-INF/manager.html");
	}
	
	@ActionKey("/unlock")
	public void ublock(){
		
		redirect("/manager");
		HttpSession session = getSession();
		String pass = getPara("pass");
		pass = SHA256Util.hash(pass); 
		
		
		if(!Admin.admin.find("select * from admin where password = ?",pass).isEmpty()){
			session.setAttribute("login", true);
			renderJson("msg", "success");
		}else{
			renderJson("msg", "fail");
		}
	
	}
	
	@ActionKey("/reset")
	public void reset(){
		log.info("重置密码");
		Admin admin = Admin.admin.findFirst("select * from admin where name = 'admin'");
		if(admin == null ){
			new Admin().set("name", "admin").set("password", SHA256Util.hash("cuitiotu")).save();
		}else{
			admin.set("password", SHA256Util.hash("cuitiotu")).update();
		}
		Log.me.saveMsg("重置密码成功");
		Alarm.sendMessage(3289, "#password#=cuitiotu");
		redirect("/lock");
	}
	
	
	@ActionKey("/lock")
	public void lockView(){
		log.debug("进入lockView");
		render("/WEB-INF/lock.html");
	}
	
	
	
	@ActionKey("/save1")
	public void saveParam(){
		
		PropertyUtil prop = new PropertyUtil("config.properties", "UTF-8");
		prop.set("normalTemperature", getPara("normaltem"));
		prop.set("normalHumidity", getPara("normalhum"));
		prop.set("phone", getPara("phone"));
		prop.set("interval", getPara("interval"));
		prop.set("secondSpace", getPara("secondSpace"));
		prop.store();
		Log.me.saveMsg("修改配置文件");
		renderJson("msg","success");
	}
	
	@ActionKey("/save2")
	public void saveAdmin(){
		PropertyUtil prop = new PropertyUtil("config.properties", "UTF-8");
		
		if(getPara("pass") != null && !getPara("pass").trim().equals("")){
			new Admin().set("name", "admin")
			.set("password", SHA256Util.hash(getPara("pass")))
			.update();
			Alarm.sendMessage(3289, "#password#="+getPara("pass"));
		}
		prop.set("appKey", getPara("appkey"));
		prop.store();
		Log.me.saveMsg("修改配置文件");
		renderJson("msg","success");
	}
}
